package com.controller;

import com.entity.TestQuestion;
import com.form.question.AddQuestionForm;
import com.form.question.ModifyQuestionForm;
import com.form.question.QuestionPageForm;
import com.service.TestQuestionService;
import com.vo.Msg;
import com.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * Version: V1.0    <br/>
 * Datetime:   2022/5/15 16:46   <br/>
 * Description: 试题控制器
 *
 * @author: chen
 */
@RestController
@RequestMapping(value = "/question")
public class QuestionController {

    @Autowired
    private TestQuestionService testQuestionService;


    /**
     * 添加试题
     *
     * @param addQuestionForm 试题表单
     * @return 添加结果
     */
    @PostMapping(value = "/add")
    public Msg add(@RequestBody AddQuestionForm addQuestionForm) {
        TestQuestion generate = addQuestionForm.generate();
        generate.setCreateTime(new Date());
        int insert = testQuestionService.insert(generate);
        return insert > 0 ? Msg.success() : Msg.error();
    }

    /**
     * 删除试题
     *
     * @param id 试题id
     * @return 删除结果
     */
    @PostMapping(value = "/delete")
    public Msg deleteQuestion(Integer id) {
        return testQuestionService.deleteQuestionById(id) ? Msg.success() : Msg.error();
    }

    /**
     * 修改试题
     *
     * @param addQuestionForm 试题表单
     * @return 修改结果
     */
    @PostMapping(value = "/modify")
    public Msg update(@RequestBody ModifyQuestionForm modifyQuestionForm) {
        TestQuestion modify = modifyQuestionForm.generate();
        return testQuestionService.modify(modify) ? Msg.success() : Msg.error();
    }

    @GetMapping("/getById")
    public Msg getQuestionById(Integer id) {
        if (id == null) {
            return Msg.error("id不能为空");
        }
        TestQuestion questionById = testQuestionService.getQuestionById(id);
        if (questionById == null) {
            return Msg.error("没有找到该试题");
        }
        return Msg.success(questionById);
    }

    /**
     * 分页获取试题
     *
     * @param questionPageForm 试题分页表单
     * @return 试题分页结果
     */
    @GetMapping(value = "/page")
    public Msg pageListQuestion(QuestionPageForm questionPageForm) {
        PageVo<TestQuestion> pageVo = testQuestionService.pageListQuestion(questionPageForm);
        return Msg.success(pageVo);
    }

    /**
     * 获取试题
     */
    @GetMapping(value = "/getAll")
    public Msg getAllQuestion() {
        return Msg.success(testQuestionService.getAllQuestion());
    }
}